package com.spcloud.service;

import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import com.netflix.loadbalancer.BestAvailableRule;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.WeightedResponseTimeRule;
import com.spcloud.service.config.kafka.MessageBinder;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

/**
 * User 业务
 * @author GuoChunbo
 * @create 2020-02-18 14:20
 * @Description: 类的描述
 */
@SpringCloudApplication
//一个注解相当于三个注解
//@SpringBootApplication springboot应用
//@EnableDiscoveryClient 客户端
//@EnableCircuitBreaker 启用断路器
@EnableHystrix
@EnableHystrixDashboard
//@EnableJms //开启消息
@EnableBinding(MessageBinder.class) //开启消息通道的绑定
public class UserApplication {

    public static void main(String[] args) {
        SpringApplication.run(UserApplication.class,args);
    }

    //注入 服务调用对象
    @LoadBalanced
    @Bean
    public RestTemplate restTemplate(){
        return  new RestTemplate();
    }

    /**
     * 负载均衡策略设置
     */
    @Bean
    public IRule iRule(){
        //RoundRobinRule 默认轮询
        //RandomRule 随机策略
        //WeightedResponseTimeRule 权重
        //BestAvailableRule 最空闲
        return new BestAvailableRule();
    }

    @Bean
    public ServletRegistrationBean getServlet(){
        //注册断路器servlet  断路器面板
        HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
        registrationBean.setLoadOnStartup(1);
        registrationBean.addUrlMappings("/actuator/hystrix.stream");
        registrationBean.setName("HystrixMetricsStreamServlet");
        return registrationBean;
    }
}
